package com.huatai.saas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.config.MinioAPI;
import com.huatai.common.constant.MinioCommonBucket;
import com.huatai.common.util.PaginationUtils;
import com.huatai.common.util.StringUtil;
import com.huatai.saas.dto.BackFileListQueryDto;
import com.huatai.saas.entity.FileListEntity;
import com.huatai.saas.mapper.FileListMapper;
import com.huatai.saas.service.FileListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: nanming-saas
 * @BelongsPackage: com.huatai.saas.service.impl
 * @Author: hecaiy
 * @CreateTime: 2024-04-11  19:30
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class FileListServiceImpl extends ServiceImpl<FileListMapper,FileListEntity> implements FileListService {

	@Autowired
	private MinioAPI minioAPI;

	/**
	 * 分页获取数据
	 * @param dto
	 * @return
	 */
	public IPage<FileListEntity> getFileListPage(BackFileListQueryDto dto){
		IPage<FileListEntity> page = PaginationUtils.getPage(dto.getPage(),dto.getSize());
		LambdaQueryWrapper<FileListEntity> qw = new LambdaQueryWrapper<FileListEntity>();
		qw.like(StringUtil.isNotBlank(dto.getFileName()), FileListEntity::getFileName, dto.getFileName());
		IPage<FileListEntity> fileListEntityIPage = this.baseMapper.selectPage(page, qw);
		List<FileListEntity> records = fileListEntityIPage.getRecords();
		records.stream().forEach(data->{
			String path = minioAPI.getCOMINFO()+"/"+ MinioCommonBucket.HUATAI_PUB_BUCKET_URL+data.getPath();
			data.setUrl(path);
		});
		return fileListEntityIPage;
	}

	/**
	 * 删除已上传文件
	 * @param id
	 * @return
	 */
	public Boolean delFileListByIds(List<Long> id) {
		return this.baseMapper.deleteBatchIds(id) > 0;
	}

}
